{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "29835de4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "['o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'a', 'p']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "# Date: week07 列表练习 & 初始字典\n",
    "\n",
    "# 目标： “Don't panic” ==> \"on tap\"\n",
    "\n",
    "# 方法一： 使用list的方法完成\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)  # list() 可以将 str  to(转)  list\n",
    "print(phrase)\n",
    "print(plist)\n",
    "\n",
    "# 1.删除后面四个元素 a n i c\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "print(plist)\n",
    "# 2. 删除 D  ‘\n",
    "plist.remove('D')\n",
    "print(plist)\n",
    "plist.pop(2)\n",
    "print(plist)\n",
    "\n",
    "# 3. 数学案例： 计算 （(a+b)*(c+d)）\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "#['o','n','t',' '][ a    ,   p     ]\n",
    "print(plist)\n",
    "\n",
    "# 4. 数据插入  实现：’t‘ 和’ ‘ 的位置转换\n",
    "# plist.insert(2,plist.pop(3))\n",
    "# print(plist)\n",
    "# print(plist.pop(2))  # 1，代表删除的内容的值 2，他删除了plist的这个值\n",
    "plist.insert(3,plist.pop(2))\n",
    "print(plist)\n",
    "# 5. list to str\n",
    "new_phrase = ''.join(plist)\n",
    "\n",
    "\n",
    "\n",
    "print(plist)\n",
    "print(new_phrase)\n",
    "\n",
    "# plist 改变了原先的状态么？ 列表的方法会改变列表本身，如果不想打破或者想保留原先数据的内容，请先用copy()复制数据\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e9021c9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bf7ff005",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "# Date: week07 列表练习 & 初始字典\n",
    "# 方法二： 使用切片的方法实现\n",
    "\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)  # list() 可以将 str  to(转)  list\n",
    "print(phrase)\n",
    "print(plist)\n",
    "\n",
    "\n",
    "on_str = ''.join(plist[1:3])\n",
    "print(on_str)\n",
    "new_phrase = on_str+ ''.join(plist[5]+plist[4]+plist[7]+plist[6])\n",
    "print(plist)\n",
    "print(new_phrase)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94a0d8d7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "57f943bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的余额：20000\n",
      "------goods list-------\n",
      "['10001', 'python课本', '88']\n",
      "['10002', '蜜雪冰城—冰茶', '10']\n",
      "['10003', 'iphone13 pro', '9999']\n",
      "['10004', '华为电-跑车', '400000']\n",
      "['10005', '小米手环', '199']\n",
      "\n",
      "请输入商品的ID：10004\n",
      "['10001', 'python课本', '88']\n",
      "您输入的商品信息有问题，请输入正确的商品ID\n",
      "['10002', '蜜雪冰城—冰茶', '10']\n",
      "您输入的商品信息有问题，请输入正确的商品ID\n",
      "['10003', 'iphone13 pro', '9999']\n",
      "您输入的商品信息有问题，请输入正确的商品ID\n",
      "['10004', '华为电-跑车', '400000']\n",
      "['10005', '小米手环', '199']\n",
      "您输入的商品信息有问题，请输入正确的商品ID\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "# Date: week07 列表练习 & 初始字典\n",
    "\n",
    "# 购物车程序项目：(20分钟)\n",
    "# 要求：1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "# 　　  2、让用户输入商品编号进行商品的购买。\n",
    "# 　　  3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "# 　　  4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。\n",
    "\n",
    "goods = [\n",
    "    [\"10001\",\"python课本\",\"88\"],\n",
    "    [\"10002\",\"蜜雪冰城—冰茶\",\"10\"],\n",
    "    [\"10003\",\"iphone13 pro\",\"9999\"],\n",
    "    [\"10004\",\"华为电-跑车\",\"400000\"],\n",
    "    [\"10005\",\"小米手环\",\"199\"]\n",
    "]\n",
    "user_shop_list = []\n",
    "\n",
    "支付宝余额 = input(\"请输入您的余额：\")\n",
    "info = '''------goods list-------\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "'''%(goods[0],goods[1],goods[2],goods[3],goods[4])\n",
    "print(info)\n",
    "user_input_ID = input(\"请输入商品的ID：\")\n",
    "for good in goods:\n",
    "    print(good)\n",
    "    # 判断商品的id是否正确\n",
    "    if user_input_ID == good[0]:\n",
    "        # 判断用户的余额是否够用\n",
    "        if int(good[2]) < int(支付宝余额):\n",
    "            # 把商品加进list\n",
    "            user_shop_list.append(good)\n",
    "            # 扣除支付宝余额\n",
    "            break\n",
    "\n",
    "    else:\n",
    "        print(\"您输入的商品信息有问题，请输入正确的商品ID\")\n",
    "\n",
    "print(user_shop_list)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b4e6267",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
